<template>
  <div class="app-container">
    <div class="menu-container">
      <el-form :inline="true" style="float: left;">
        <el-button size="mini" type="primary" @click="createPermission()">添加管理员</el-button>
      </el-form>
      <div style="clear:both"/>
    </div>
    <el-table v-loading="listLoading" :data="page.records" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="登录账号">
        <template slot-scope="scope">
          <el-button type="text" size="small" icon="el-icon-edit" @click="modifyBMSUser(scope.row)">
            {{ scope.row.username }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column width="120px" align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.first_name }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="权限组">
        <template slot-scope="scope">
          <span>{{ scope.row.role_names }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.is_active_desc }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="加入时间">
        <template slot-scope="scope">
          <span>{{ scope.row.date_joined }}</span>
        </template>
      </el-table-column>

      <el-table-column width="120px" label="最后登录时间">
        <template slot-scope="scope">
          <span>{{ scope.row.last_login }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="resetUserPassword(scope.row)">重置密码</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delBMSUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="page.recordsTotal>0" :total="page.recordsTotal" :page.sync="listQuery.pageNum" :page-sizes="[20, 50, 100]" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :visible.sync="dialogFormVisible" :title="dialogTitle" log- width="80%">
      <bMSUser-detail :is-edit="isEdit" :current-id="currentBMSUserId" @successBMSUser="successSaveBMSUser"/>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, deleteBMSUser, resetPassword } from '@/api/bmsuser'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import BMSUserDetail from './components/BMSUserDetail'

export default {
  name: 'PermissionsList',
  components: { Pagination, BMSUserDetail },
  data() {
    return {
      dialogTitle: '新增权限',
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20
      },
      isEdit: false,
      currentBMSUserId: undefined,
      formLabelWidth: '100px',
      dialogFormVisible: false,
      page: { pageNum: 1, pageSize: 20, pageTotal: 0, recordsTotal: 0, records: [] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    successSaveBMSUser() {
      this.dialogFormVisible = false
      this.getList()
    },
    createPermission() {
      this.isEdit = false
      this.currentBMSUserId = undefined
      this.dialogTitle = '添加权限'
      this.dialogFormVisible = true
    },
    modifyBMSUser(user) {
      this.isEdit = true
      this.currentBMSUserId = user.id
      this.dialogTitle = '编辑权限'
      this.dialogFormVisible = true
    },
    resetUserPassword(user) {
      resetPassword({ id: user.id }).then(response => {
        this.$alert('重置后的密码为：' + response.data.data.now_password, '重置成功', {
          confirmButtonText: '确定',
          callback: action => {
          }
        })
      })
    },
    delBMSUser(user) {
      deleteBMSUser(user.id).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
          return
        }
        this.$message({ 'message': '删除成功', 'type': 'success' })
        this.getList()
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        const data = response.data
        if (data.code !== 0) {
          this.$message.error(data.msg)
        }
        this.page = data.data
        this.listLoading = false
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
